/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IPSecPolicyForm.java
 *
 * Created on 28 févr. 2011, 16:37:52
 */

package gui;

import java.awt.event.ActionEvent;
import javax.swing.JButton;
import model.IPSecPolicy;
import model.IPSecRule;

/**
 *
 * @author Dudu
 */
public class IPSecPolicyForm extends javax.swing.JPanel {
    private IPSecPolicy policy;
    MainFrame parentFrame;
    /** Creates new form IPSecPolicyForm */
    public IPSecPolicyForm(IPSecPolicy policy,MainFrame parentFrame) {
        this.policy=policy;
        this.parentFrame=parentFrame;
        initComponents();
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jButton3 = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        jDialog2 = new javax.swing.JDialog();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jComboBox3 = new javax.swing.JComboBox();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jComboBox4 = new javax.swing.JComboBox();
        jComboBox5 = new javax.swing.JComboBox();
        jComboBox6 = new javax.swing.JComboBox();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jDialog1.getContentPane().setLayout(new java.awt.BorderLayout(20, 0));

        jButton3.setText("jButton3");
        jButton3.setBorder(null);
        jButton3.setMargin(new java.awt.Insets(0, 50, 0, 50));
        jButton3.setMaximumSize(new java.awt.Dimension(50, 23));
        jButton3.setMinimumSize(new java.awt.Dimension(50, 23));
        jButton3.setModel(null);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jDialog1.getContentPane().add(jButton3, java.awt.BorderLayout.PAGE_END);

        jLabel12.setText("Veuillez rentrer tous les paramètres pour créer un nouvelle IPSec policy");
        jLabel12.setBorder(javax.swing.BorderFactory.createEmptyBorder(20, 10, 20, 10));
        jDialog1.getContentPane().add(jLabel12, java.awt.BorderLayout.CENTER);

        jDialog2.getContentPane().setLayout(new java.awt.FlowLayout());

        setPreferredSize(new java.awt.Dimension(819, 539));

        jLabel2.setText("Source");

        jLabel3.setText("Destination");

        jLabel4.setText("Protocol");

        jLabel5.setText("Direction");

        jLabel6.setText("Action");

        jLabel7.setText("tunnel_in");

        jLabel8.setText("tunnel_out");

        jLabel9.setText("Type");

        jLabel10.setText("Transport Type");

        jLabel11.setText("Level");

        jTextField2.setText(this.getPolicy().getSource());

        jTextField3.setText(this.getPolicy().getDestination());

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Discard", "None", "IPSec" }));
        jComboBox3.setSelectedItem(this.getPolicy().getAction());

        jTextField4.setText(this.getPolicy().getTunnel_in());

        jTextField5.setText(this.getPolicy().getTunnel_out());

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "AH", "ESP" }));
        jComboBox4.setSelectedItem(this.getPolicy().getType());

        jComboBox5.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Tunnel", "Transport" }));
        jComboBox5.setSelectedItem(this.getPolicy().getTransport_type());

        jComboBox6.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Default", "Use", "Require", "Unique" }));
        jComboBox6.setSelectedItem(this.getPolicy().getLevel());
        jComboBox6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox6ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "TCP", "UDP" }));
        jComboBox1.setSelectedItem(this.getPolicy().getProtocol());

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "IN", "OUT", "FORWARD" }));
        jComboBox2.setSelectedItem(this.getPolicy().getDirection());

        jButton1.setText(this.setButtonLabel1());
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(this.setButtonLabel2());
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11)
                            .addComponent(jLabel7)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(130, 130, 130)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE)
                            .addComponent(jComboBox3, 0, 418, Short.MAX_VALUE)
                            .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE)
                            .addComponent(jTextField5, javax.swing.GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE)
                            .addComponent(jComboBox4, 0, 418, Short.MAX_VALUE)
                            .addComponent(jComboBox6, 0, 418, Short.MAX_VALUE)
                            .addComponent(jComboBox5, javax.swing.GroupLayout.Alignment.TRAILING, 0, 418, Short.MAX_VALUE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE)
                            .addComponent(jComboBox1, 0, 418, Short.MAX_VALUE)
                            .addComponent(jComboBox2, 0, 418, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(57, 57, 57)))
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addGap(59, 59, 59)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox6ActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jComboBox6ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox6ActionPerformed

    private void jButton2ActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if(policy.getName()==null){
           parentFrame.setRightPanel("IPSec policies");
        }
        else{
            parentFrame.remove(policy);
            parentFrame.deleteIPSecPolicy(policy);

        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        if(policy.getName()==null){
            if(jTextField2.getText()!=null&&jTextField3.getText()!=null&&jComboBox1.getSelectedItem()!=null&&jComboBox2.getSelectedItem()!=null&&jComboBox3.getSelectedItem()!=null&&jTextField4.getText()!=null&&jTextField5.getText()!=null&& jComboBox4.getSelectedItem()!=null&&jComboBox5.getSelectedItem()!=null&&jComboBox6.getSelectedItem()!=null){
                IPSecPolicy p=new IPSecPolicy(null,jTextField2.getText(),jTextField3.getText(),(String) jComboBox1.getSelectedItem(),(String) jComboBox2.getSelectedItem(),(String) jComboBox3.getSelectedItem(),jTextField4.getText(),jTextField5.getText(),(String) jComboBox4.getSelectedItem(),(String) jComboBox5.getSelectedItem(),(String) jComboBox6.getSelectedItem());
                parentFrame.register(p);
                parentFrame.addIPSecPolicy(p);

            }
        }
 else{
         parentFrame.modifyIPSecPolicy(policy,new IPSecPolicy(null,jTextField2.getText(),jTextField3.getText(),(String) jComboBox1.getSelectedItem(),(String) jComboBox2.getSelectedItem(),(String) jComboBox3.getSelectedItem(),jTextField4.getText(),jTextField5.getText(),(String) jComboBox4.getSelectedItem(),(String) jComboBox5.getSelectedItem(),(String) jComboBox6.getSelectedItem()));
 }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        jDialog1.setVisible(false);
}//GEN-LAST:event_jButton3ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JComboBox jComboBox5;
    private javax.swing.JComboBox jComboBox6;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JDialog jDialog2;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    // End of variables declaration//GEN-END:variables

    public IPSecPolicy getPolicy() {
        return policy;
    }
    private String setButtonLabel1(){
        if(policy.getName()==null){
            return "Create policy";
        }
    else{return "Modify policy";

    }
    }
    private String setButtonLabel2(){
        if(policy.getName()==null){
            return "Cancel policy creation";
        }
    else{return "Delete policy";
    }
    }

    public JButton getjButton1() {
        return jButton1;
    }

    public JButton getjButton2() {
        return jButton2;
    }

}
